package cn.kgc.jinxin.clothesstore.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.kgc.jinxin.clothesstore.dao.BaseDao;
import cn.kgc.jinxin.clothesstore.dao.CategoryDao;
import cn.kgc.jinxin.clothesstore.entity.Category;

public class CategoryDaolmpl implements  CategoryDao{

	@Override
	public List<Category> selectAll() throws Exception {
		List<Category> categoryList = new ArrayList<>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "select cateId,cateName from category";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			Category category;
			while (resultSet.next()) {
				category = new Category();
				category.setCateid(resultSet.getInt(1));
				category.setCateName(resultSet.getString(2));
				category.setCateParent((Category)resultSet.getObject(3));
				categoryList.add(category);
			} 
		} finally {
			BaseDao.close(connection, preparedStatement, resultSet);
		}
		return categoryList;
	}

	@Override
	public Category selectById(Integer id) throws Exception {
		Category category = null;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "select cateid,catename from category where cateid=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				category = new Category();
				category.setCateid(resultSet.getInt(1));
				category.setCateName(resultSet.getString(2));
			} 
		} finally {
			BaseDao.close(connection, preparedStatement, resultSet);
		}
		return category;
	}

	@Override
	public void insert(Category category) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "insert into category VALUES (?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, category.getCateid());
			preparedStatement.setString(2, category.getCateName());
			preparedStatement.setInt(3, category.getCateParent().getCateid());
			preparedStatement.executeUpdate();
		} finally {
			BaseDao.close(connection, preparedStatement, null);
		}		
	}

	@Override
	public void update(Category category) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "update category set catename=? where cateid=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, category.getCateName());
			preparedStatement.setInt(2, category.getCateid());
			preparedStatement.executeUpdate();
		} finally {
			BaseDao.close(connection, preparedStatement, null);
		}
		
		
	}

	@Override
	public void deleteById(Integer cateId) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "delete from category where cateid=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, cateId);
			preparedStatement.executeUpdate();
		} finally {
			BaseDao.close(connection, preparedStatement, null);
		}
		
	}

}
